import {Injectable} from '@angular/core';
import {SettingUrl} from '../../public/setting/setting_url';
import {HttpCodesEnum} from '../../public/setting/enums';
import {NzMessageService, NzNotificationService} from 'ng-zorro-antd';
import {AjaxService} from '../../public/service/ajax.service';

@Injectable({
  providedIn: 'root'
})
export class FinanceService {

  constructor(private notification: NzNotificationService,
              private ajaxService: AjaxService,
              private message: NzMessageService) {
  }


  /**
   * 充值
   * @param params
   * @returns {Promise<T>}
   */
  addBalance(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.platform.addBalance,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 扣除
   * @param params
   * @returns {Promise<T>}
   */
  deductBalance(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.platform.deductBalance,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 查询ActiveRecord列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getActiveRecordList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.activeRecord.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }
}
